package com.swu.pad.course.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.swu.pad.course.dto.Student;
import com.swu.pad.course.entity.Course;
import com.swu.pad.course.entity.CourseGroupUser;
import com.swu.pad.course.entity.Group;
import com.swu.pad.course.service.CourseService;
import com.swu.pad.system.dto.ResultDto;
import com.swu.pad.system.entity.User;

@Controller
@RequestMapping(value = "/course")
public class CourseController {

	@Autowired
	private CourseService courseServce;

	@ResponseBody
	@RequestMapping(value = "/createCourse")
	public String createCourse(Course course, @RequestParam(value = "importStudent", required = false) MultipartFile file, HttpSession session) {
		User user = (User) session.getAttribute("user");
		course.setTeacherId(user.getId());
		course.setTeacherName(user.getRealname());
		course = courseServce.addCourse(course);
		if (file != null && !file.isEmpty()) {
			courseServce.importStudent(course, file);
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteCourse")
	public String deleteCourse(String courseId) {
		courseServce.deleteCourse(courseId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/updateCourse")
	public String updateCourse(Course course, HttpSession session) {
		User user = (User) session.getAttribute("user");
		course.setTeacherId(user.getId());
		course.setTeacherName(user.getRealname());
		course = courseServce.addCourse(course);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getCourses")
	public List<Course> getCourses(String userId, HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (User.ROLE_STUDENT.equals(user.getRole())) {
			return courseServce.getStudentCourse(user.getId());
		} else {
			return courseServce.getTeacherCourse(user.getId());
		}
	}

	@ResponseBody
	@RequestMapping(value = "/getCourse")
	public Course getCourse(String courseId) {
		return courseServce.getCourseById(courseId);
	}

	@ResponseBody
	@RequestMapping(value = "/getCourseDetail")
	public Map<String, Object> getCourseDetail(String courseId) {
		return courseServce.getCourseDetail(courseId);
	}

	@ResponseBody
	@RequestMapping(value = "/getCourseGroup")
	public List<Group> getCourseGroup(String courseId) {
		return courseServce.getCourseGroup(courseId, Group.TYPE_GROUP);
	}

	@ResponseBody
	@RequestMapping(value = "/getUserAssignmentGroup")
	public List<CourseGroupUser> getUserAssignmentGroup(String userId, String assignmentId) {
		return courseServce.getUserAssignmentGroup(userId, assignmentId, Group.TYPE_GROUP);
	}

	@ResponseBody
	@RequestMapping(value = "/getStudents")
	public ResultDto getStudents(String courseId) {
		List<Student> studentList = courseServce.getStudents(courseId);
		ResultDto result = new ResultDto();
		result.setCode(ResultDto.CODE_OK);
		result.setMessage("ok");
		result.setData(studentList);
		return result;
	}

	/**
	 * 修改学生信息
	 * @param courseId
	 * @param student
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateStudent")
	public ResultDto updateStudent(String courseId, Student student) {
		courseServce.updateStudent(courseId, student);
		ResultDto result = new ResultDto();
		result.setMessage("ok");
		return result;
	}
	@ResponseBody
	@RequestMapping(value = "/deleteStudent")
	public ResultDto deleteStudent(String courseId, String userId) {
		courseServce.deleteStudent(courseId, userId);
		ResultDto result = new ResultDto();
		result.setMessage("ok");
		return result;
	}

	/**
	 * 获取课程的所有小组
	 * @param courseId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getGroups")
	public List<Group> getGroups(String courseId) {
		return courseServce.getCourseGroup(courseId, Group.TYPE_GROUP);
	}
	
	
	/**
	 * 获取小组学生
	 * @param courseId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getStudentsByGroupId")
	public ResultDto getStudentsByGroupId(String groupId) {
		List<Student> studentList = courseServce.getStudentsByGroupId(groupId);
		ResultDto result = new ResultDto();
		result.setCode(ResultDto.CODE_OK);
		result.setMessage("ok");
		result.setData(studentList);
		return result;
	}
}
